package fangzhiyi.homework;

import java.util.Random;

/**
 * @author fangzhiyi
 * @version 1.0
 * @date 2025/3/18 11:51
 * @description 矩形的面积和周长
 */
public class Test01 {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle();
        rectangle.setChang(-45);
        rectangle.setWide(-85);
        System.out.println("rectangle.getChang() = " + rectangle.getChang());
        System.out.println("rectangle.getWide() = " + rectangle.getWide());
        rectangle.rectangleArea();
        rectangle.rectangleGirth();
        rectangle.rectangleShow();

        Random random = new Random();
      Rectangle[] rec = new Rectangle[5];
        for (int i = 0; i < 5; i++) {
            double chang = random.nextDouble()*9+1;
            double kuan = random.nextDouble()*9+1;
            rec[i] = new Rectangle(chang,kuan);
        }
        for (Rectangle rectangle1 : rec) {
            System.out.println(rectangle1.rectangleShow());
        }

        //冒泡排序
        for (int i = 0; i < rec.length - 1; i++) {
            for (int j = 0; j < rec.length-1-i; j++) {
                if (rec[j].rectangleGirth()>rec[j+1].rectangleGirth()){
                    Rectangle temp = rec[j];
                    rec[j] = rec[j+1];
                    rec[j+1] = temp;
                }
            }
        }
        System.out.println("-------------------------------------------------------------------");
        for (Rectangle rectangle1 : rec) {
            System.out.println(rectangle1.rectangleShow());
        }
    }
}

class Rectangle{
    private double chang;
    private double wide;

    public void setChang(double chang) {
        if (chang<0){
           chang = 1;
        }
        this.chang = chang;
    }
    public double getChang() {
        return chang;
    }

    public double getWide() {
        return wide;
    }

    public void setWide(double wide) {
        if (wide<0){
            wide = 1;
        }
        this.wide = wide;
    }

    public Rectangle() {
    }

    public Rectangle(double chang, double wide) {
        this.chang = chang;
        this.wide = wide;
    }

    public double rectangleArea(){
       return chang * wide;
    }
    public double rectangleGirth(){
        return 2*(chang+wide);
    }
    public String rectangleShow(){
        return "矩形的长为："+chang+",\t矩形的宽为:"+wide+",\t面积为"+rectangleArea()+",\t周长为"+rectangleGirth();
    }

}
